package de.breitbeil.jbooking.ui.form;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.HashMap;
import java.util.Observable;

import javax.swing.JComponent;

import org.springframework.core.closure.Constraint;
import org.springframework.richclient.form.AbstractForm;
import org.springframework.richclient.form.binding.swing.SwingBindingFactory;
import org.springframework.richclient.form.builder.TableFormBuilder;

import de.breitbeil.jbooking.domain.Booking;
import de.breitbeil.jbooking.domain.Event;
import de.breitbeil.jbooking.domain.Person;
import de.breitbeil.jbooking.richclient.form.binding.ComboBoxDaoBinder;
import de.breitbeil.jbooking.util.NullAwarePropertyComparator;
import de.breitbeil.jbooking.util.PersonFilter;

public class EventForm extends AbstractForm {

	public EventForm(Event event) {
		super(event);
		setId("eventForm");
	}
	
	@Override
	protected JComponent createFormControl() {
		SwingBindingFactory bindingFactory = (SwingBindingFactory) getBindingFactory();
		TableFormBuilder formBuilder = new TableFormBuilder(bindingFactory);
		formBuilder.setLabelAttributes("colGrId=label colSpec=right:pref");
		
		formBuilder.addSeparator(getMessage(getId()+".eventSeparator.label"));
		formBuilder.row();
        formBuilder.add("comment", "colspan=2");
		formBuilder.row();
		formBuilder.add("deadline", "colspan=1");
		formBuilder.row();
		formBuilder.add("done");
//		formBuilder.row();
		formBuilder.add("date_done");
		formBuilder.row();
		formBuilder.addSeparator(getMessage(getId()+".relationSeparator.label"));
		formBuilder.row();
		formBuilder.add("person");
		formBuilder.row();
		formBuilder.addBinding(bindingFactory.createBinding("booking", createBookingContext()), "", "");
		
		return formBuilder.getForm();
	}
	
	protected HashMap<String, Object> createBookingContext() {
	    HashMap<String, Object> bookingContext = new HashMap<String, Object>();
	    PersonFilter personFilter = new PersonFilter(getFormModel().getValueModel("person"));
	    
        getFormModel().getValueModel("person").addValueChangeListener(personFilter);
	    
        bookingContext.put(ComboBoxDaoBinder.FILTER_KEY, personFilter);
        bookingContext.put(ComboBoxDaoBinder.COMPARATOR_KEY, new NullAwarePropertyComparator("from", false, false));
        
        return bookingContext;
	}

}
